import { useState, useEffect, useRef } from 'react';

export function useComputedValue(computeFunc: any, deps: any[]) {
    const [value, setValue] = useState(computeFunc());
    useEffect(() => {
        setValue(computeFunc());
    }, deps);
    return value;
}

//同步修改state，异步渲染(史)
export function useSyncState<T>(initialValue: T): [T, T, (newvalue: T) => void] {
    const ref = useRef<T>(initialValue)
    const [state, setValue] = useState<T>(initialValue)
    const setSyncState = (newValue: T) => {
        ref.current = newValue;
        setValue(newValue)
    }
    useEffect(() => {
        ref.current = initialValue
        setValue(initialValue)
    }, [initialValue])
    return [ref.current, state, setSyncState]
}

